const {
    Store
} = require('../utils/store')
const {
    I18n
} = require('../utils/i18n')
const locales = require('../locales/index');

const StorageKey = 'i18n_lang';
const storageLang = wx.getStorageSync(StorageKey) || 'zh';

const i18n = new I18n({
    defaultLanguage: storageLang,
    languages: {
        ...locales
    },
})
class I18nStore extends Store {
    constructor() {
        super()
        this.data = {
            i18n: i18n.getLanguages()
        };
        this.intl = i18n;
    }
    loadI18n(callback) {
        this.data.i18n = i18n.getLanguages();
        this.update('i18nStore', callback);
    }
    getLanguage() {
        return i18n.getLanguage();
    }
    setLanguage(lang) {
        i18n.setLanguage(lang);
        this.loadI18n();
        wx.setStorageSync(StorageKey, lang)
    }
}

module.exports = new I18nStore